﻿@inject IHttpService _httpService
@inject IDataCacheService _dataCacheService
@inject NavigationManager NavigationManager
@inject IPageModelCatche<List<HotRecommendItemModel>> _pageModelCatche

<section class="published-games-card">
    <CnGalWebSite.Shared.MasaComponent.PC.Home.Cards.Shared.SlideGroup Title="随机推荐" MoreText="换一批" MoreIcon="IconType.Refresh" ForceShowMore OnClickMore="OnClickMore" Items="_items.Take(_dataCacheService.IsApp?9:16)">
         <MDCard Class="item" Href="@context.Url">
             <img src="@context.Image" alt="@context.Name" loading="lazy" />
             <div class="content">
                 <div class="name text-truncate-1 title-large">@context.Name</div>
                 <div class="brief text-truncate-3 label-large">@context.BriefIntroduction</div>
                 <div class="tags">
                     <CnGalWebSite.Shared.MasaComponent.PC.Admin.Recommends.ReasonChip Reason="context.Reason" />
                 </div>
             </div>
         </MDCard>
     </CnGalWebSite.Shared.MasaComponent.PC.Home.Cards.Shared.SlideGroup>
 </section>

 @code {

    List<HotRecommendItemModel> _items { get; set; } = new List<HotRecommendItemModel>();

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }


    protected override async Task OnInitializedAsync()
    {
        try
        {
            _items = await _pageModelCatche.GetCache("api/home/ListHotRecommends");
            StateHasChanged();
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取主页游戏精选列表失败");
        }
    }


    public async Task OnClickMore()
    {
        try
        {
            _pageModelCatche.Clean("api/home/ListHotRecommends");
            _items = await _pageModelCatche.GetCache("api/home/ListHotRecommends");
            StateHasChanged();
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取主页游戏精选列表失败");
        }
    }
}
